
import Taro from "@tarojs/taro";
import { Control, FabricImage } from "fabric";
import FabricImg from "./FabricImg";

// 删除控制器
class TlContorls extends Control {
    constructor(options) {
        super(options);
    }
    icon: FabricImg;
    x = -0.5;
    y = -0.5;
    cursorStyle = 'crosshair';
    mouseUpHandler = (eventData, transform, x, y) => {
        Taro.showModal({
            title: "删除", // 标题
            content: "是否要删除", // 内容
            showCancel: true, // 不显示取消按钮（设为 true 则显示两个按钮）
            confirmText: "确定", // 确认按钮文字
            success: (res) => {
                if (res.confirm) {
                    const canvas = transform.target.canvas;
                    const target = transform.target;
                    if (canvas) {
                        // 执行删除
                        // 如果 target 是一个组，则需要删除所有子对象
                        if (target.type === 'activeselection') {
                            const objs = target.getObjects();
                            objs.forEach((obj) => {
                                canvas.remove(obj);
                            });
                        }
                        canvas.remove(target);
                        canvas.discardActiveObject();
                        canvas.requestRenderAll(); // 必须重新渲染
                    }
                }
            }
        });
    }
    // actionHandler(eventData, transform, x, y) {
    // },
    render = (ctx, left, top, styleOverride, fabricObject) => {
        if (this.icon) {
            this.icon.set({
                originX: "center",
                originY: "center",
                left,
                top,
                // angle: fabricObject.angle
            });
            this.icon.render(ctx);
        }
    }

}

const tlContorls = new TlContorls({});

export default tlContorls;